<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>Hoodoo::Services::Discovery::ByDRb::DRbServer</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <link rel="stylesheet" href="../../../../../css/reset.css" type="text/css" media="screen" />
<link rel="stylesheet" href="../../../../../css/main.css" type="text/css" media="screen" />
<link rel="stylesheet" href="../../../../../css/github.css" type="text/css" media="screen" />
<script src="../../../../../js/jquery-1.3.2.min.js" type="text/javascript" charset="utf-8"></script>
<script src="../../../../../js/jquery-effect.js" type="text/javascript" charset="utf-8"></script>
<script src="../../../../../js/main.js" type="text/javascript" charset="utf-8"></script>
<script src="../../../../../js/highlight.pack.js" type="text/javascript" charset="utf-8"></script>

</head>

<body>
    <div class="banner">
        <h1>
            <span class="type">Class</span>
            Hoodoo::Services::Discovery::ByDRb::DRbServer
                <span class="parent">&lt;
                    Object
                </span>
        </h1>
        <ul class="files">
            <li><a href="../../../../../files/lib/hoodoo/services/discovery/discoverers/by_drb/drb_server_rb.html">lib/hoodoo/services/discovery/discoverers/by_drb/drb_server.rb</a></li>
        </ul>
    </div>
    <div id="bodyContent">
        <div id="content">
    <div class="description">
      
<p>A registry of service endpoints, implenented as a DRB server class. An
internal implementation detail of <a
href="../../Middleware.html">Hoodoo::Services::Middleware</a>, in most
respects.</p>

    </div>








    <!-- Method ref -->
    <div class="sectiontitle">Methods</div>
    <dl class="methods">
        <dt>A</dt>
        <dd>
          <ul>
              <li>
                <a href="#method-i-add">add</a>
              </li>
          </ul>
        </dd>
        <dt>F</dt>
        <dd>
          <ul>
              <li>
                <a href="#method-i-find">find</a>,
              </li>
              <li>
                <a href="#method-i-flush">flush</a>
              </li>
          </ul>
        </dd>
        <dt>N</dt>
        <dd>
          <ul>
              <li>
                <a href="#method-c-new">new</a>
              </li>
          </ul>
        </dd>
        <dt>P</dt>
        <dd>
          <ul>
              <li>
                <a href="#method-i-ping">ping</a>
              </li>
          </ul>
        </dd>
        <dt>S</dt>
        <dd>
          <ul>
              <li>
                <a href="#method-c-start">start</a>,
              </li>
              <li>
                <a href="#method-i-stop">stop</a>
              </li>
          </ul>
        </dd>
        <dt>U</dt>
        <dd>
          <ul>
              <li>
                <a href="#method-c-uri">uri</a>
              </li>
          </ul>
        </dd>
    </dl>










    <!-- Methods -->
      <div class="sectiontitle">Class Public methods</div>
        <div class="method">
          <div class="title method-title" id="method-c-new">
              <b>new</b>()
            <a href="../../../../../classes/Hoodoo/Services/Discovery/ByDRb/DRbServer.html#method-c-new" name="method-c-new" class="permalink">Link</a>
          </div>

            <div class="description">
              <p>Create an instance ready for use as a DRb “front object”.</p>
            </div>



            <div class="sourcecode">
              <p class="source-link">
                Source:
                <a href="javascript:toggleSource('method-c-new_source')" id="l_method-c-new_source">show</a>
              </p>
              <div id="method-c-new_source" class="dyn-source">
                <pre><span class="ruby-comment"># File lib/hoodoo/services/discovery/discoverers/by_drb/drb_server.rb, line 105</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">initialize</span>
  <span class="ruby-ivar">@repository</span> = {}
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
          </div>
        <div class="method">
          <div class="title method-title" id="method-c-start">
              <b>start</b>( port = nil )
            <a href="../../../../../classes/Hoodoo/Services/Discovery/ByDRb/DRbServer.html#method-c-start" name="method-c-start" class="permalink">Link</a>
          </div>

            <div class="description">
              <p>Start the DRb server. Does not return (joins the DRb thread). If the server
is already running, expect an “address in use” connection exception from
DRb.</p>

<p>$SAFE will be set to 1 (unless it is already higher) in this thread.</p>
<dl class="rdoc-list note-list"><dt><code>port</code>
<dd>
<p>Passed to <a href="DRbServer.html#method-c-uri">::uri</a> method.</p>
</dd></dl>
            </div>



            <div class="sourcecode">
              <p class="source-link">
                Source:
                <a href="javascript:toggleSource('method-c-start_source')" id="l_method-c-start_source">show</a>
              </p>
              <div id="method-c-start_source" class="dyn-source">
                <pre><span class="ruby-comment"># File lib/hoodoo/services/discovery/discoverers/by_drb/drb_server.rb, line 65</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">self</span>.<span class="ruby-identifier">start</span>( <span class="ruby-identifier">port</span> = <span class="ruby-keyword">nil</span> )

  <span class="ruby-identifier">uri</span> = <span class="ruby-keyword">self</span>.<span class="ruby-identifier">uri</span>( <span class="ruby-identifier">port</span> )

  <span class="ruby-comment"># For security, &quot;disable eval() and friends&quot;:</span>
  <span class="ruby-comment">#</span>
  <span class="ruby-comment"># http://www.ruby-doc.org/stdlib-2.2.3/libdoc/drb/rdoc/DRb.html</span>
  <span class="ruby-comment"># https://ruby-hacking-guide.github.io/security.html</span>
  <span class="ruby-comment"># http://blog.recurity-labs.com/archives/2011/05/12/druby_for_penetration_testers/</span>

  <span class="ruby-identifier">$SAFE</span> = <span class="ruby-number">1</span>

  <span class="ruby-comment"># Have to allow a tained port string from &quot;outside&quot; just to be able</span>
  <span class="ruby-comment"># to start the service on a given port; so untaint that deliberately.</span>
  <span class="ruby-comment">#</span>
  <span class="ruby-comment"># http://ruby-doc.com/docs/ProgrammingRuby/html/taint.html</span>

  <span class="ruby-identifier">uri</span>.<span class="ruby-identifier">untaint</span>()
  <span class="ruby-identifier">$stop_queue</span> = <span class="ruby-operator">::</span><span class="ruby-constant">Queue</span>.<span class="ruby-identifier">new</span>

  <span class="ruby-operator">::</span><span class="ruby-constant">DRb</span>.<span class="ruby-identifier">start_service</span>( <span class="ruby-identifier">uri</span>,
                       <span class="ruby-constant">FRONT_OBJECT</span>,
                       <span class="ruby-value">:tcp_acl</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-constant">LOCAL_ACL</span> )

  <span class="ruby-comment"># DRB.thread.exit() does not reliably work; sometimes, it just hangs</span>
  <span class="ruby-comment"># up. I don&#39;t know why. On OS X and under Travis, sporadic failures</span>
  <span class="ruby-comment"># to return from the &quot;stop()&quot; method would result. Instead, we use a</span>
  <span class="ruby-comment"># relatively elaborate queue; sit here waiting for a message to be</span>
  <span class="ruby-comment"># pushed onto it, then just let this method exit naturally, ignoring</span>
  <span class="ruby-comment"># the value that appeared on the queue.</span>
  <span class="ruby-comment">#</span>
  <span class="ruby-comment"># The sleep makes it more reliable too, indicating some kind of nasty</span>
  <span class="ruby-comment"># race condition on start-vs-wait-to-shutdown.</span>

  <span class="ruby-identifier">sleep</span>( <span class="ruby-number">1</span> )
  <span class="ruby-identifier">$stop_queue</span>.<span class="ruby-identifier">pop</span>()
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
          </div>
        <div class="method">
          <div class="title method-title" id="method-c-uri">
              <b>uri</b>( port = nil )
            <a href="../../../../../classes/Hoodoo/Services/Discovery/ByDRb/DRbServer.html#method-c-uri" name="method-c-uri" class="permalink">Link</a>
          </div>

            <div class="description">
              <p>URI for DRb server used during local machine development as a registry of
service endpoints. Whichever service starts first runs the server which
others connect to if subsequently started.</p>
<dl class="rdoc-list note-list"><dt><code>port</code>
<dd>
<p>Optional integer port number for DRb service. If specified, this is used;
else the <code>HOODOO_DISCOVERY_BY_DRB_PORT_OVERRIDE</code> environment
variable is used; else a default of 8787 is chosen. Passing
<code>nil</code> explicitly also leads to the use of the environment
variable or default value.</p>
</dd></dl>
            </div>



            <div class="sourcecode">
              <p class="source-link">
                Source:
                <a href="javascript:toggleSource('method-c-uri_source')" id="l_method-c-uri_source">show</a>
              </p>
              <div id="method-c-uri_source" class="dyn-source">
                <pre><span class="ruby-comment"># File lib/hoodoo/services/discovery/discoverers/by_drb/drb_server.rb, line 43</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">self</span>.<span class="ruby-identifier">uri</span>( <span class="ruby-identifier">port</span> = <span class="ruby-keyword">nil</span> )

  <span class="ruby-identifier">port</span> <span class="ruby-operator">||=</span> <span class="ruby-constant">ENV</span>[ <span class="ruby-string">&#39;HOODOO_DISCOVERY_BY_DRB_PORT_OVERRIDE&#39;</span> ] <span class="ruby-operator">||</span> <span class="ruby-number">8787</span>

  <span class="ruby-comment"># Use IP address, rather than &#39;localhost&#39; here, to ensure that &quot;address</span>
  <span class="ruby-comment"># in use&quot; errors are raised immediately if a second server startup</span>
  <span class="ruby-comment"># attempt is made:</span>
  <span class="ruby-comment">#</span>
  <span class="ruby-comment">#   https://bugs.ruby-lang.org/issues/3052</span>
  <span class="ruby-comment">#</span>
  <span class="ruby-node">&quot;druby://127.0.0.1:#{ port }&quot;</span>

<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
          </div>
      <div class="sectiontitle">Instance Public methods</div>
        <div class="method">
          <div class="title method-title" id="method-i-add">
              <b>add</b>( resource, version, uri )
            <a href="../../../../../classes/Hoodoo/Services/Discovery/ByDRb/DRbServer.html#method-i-add" name="method-i-add" class="permalink">Link</a>
          </div>

            <div class="description">
              <p>Add an endpoint to the list. If the endpoint was already added, it will be
overwritten with the new data.</p>
<dl class="rdoc-list note-list"><dt><code>resource</code>
<dd>
<p>Resource as a String or Symbol, e.g. “Product”</p>
</dd><dt><code>version</code>
<dd>
<p>Endpoint&#39;s implemented API version as an Integer, e.g. 1</p>
</dd><dt><code>uri</code>
<dd>
<p>URI at which this service may be accessed, including the endpoint path
(e.g. “<a
href="http://localhost:3002/v1/products">localhost:3002/v1/products</a>”),
as a String.</p>
</dd></dl>
            </div>



            <div class="sourcecode">
              <p class="source-link">
                Source:
                <a href="javascript:toggleSource('method-i-add_source')" id="l_method-i-add_source">show</a>
              </p>
              <div id="method-i-add_source" class="dyn-source">
                <pre><span class="ruby-comment"># File lib/hoodoo/services/discovery/discoverers/by_drb/drb_server.rb, line 124</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">add</span>( <span class="ruby-identifier">resource</span>, <span class="ruby-identifier">version</span>, <span class="ruby-identifier">uri</span> )
  <span class="ruby-ivar">@repository</span>[ <span class="ruby-node">&quot;#{ resource }/#{ version }&quot;</span> ] = <span class="ruby-identifier">uri</span>
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
          </div>
        <div class="method">
          <div class="title method-title" id="method-i-find">
              <b>find</b>( resource, version )
            <a href="../../../../../classes/Hoodoo/Services/Discovery/ByDRb/DRbServer.html#method-i-find" name="method-i-find" class="permalink">Link</a>
          </div>

            <div class="description">
              <p>Find an endpoint in the list. Returns URI at which the service may be
accessed as a String, or &#39;nil&#39; if not found.</p>
<dl class="rdoc-list note-list"><dt><code>resource</code>
<dd>
<p>Resource as a String or Symbol, e.g. “Product”</p>
</dd><dt><code>version</code>
<dd>
<p>Endpoint&#39;s implemented API version as an Integer, e.g. 1</p>
</dd></dl>
            </div>



            <div class="sourcecode">
              <p class="source-link">
                Source:
                <a href="javascript:toggleSource('method-i-find_source')" id="l_method-i-find_source">show</a>
              </p>
              <div id="method-i-find_source" class="dyn-source">
                <pre><span class="ruby-comment"># File lib/hoodoo/services/discovery/discoverers/by_drb/drb_server.rb, line 134</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">find</span>( <span class="ruby-identifier">resource</span>, <span class="ruby-identifier">version</span> )
  <span class="ruby-ivar">@repository</span>[ <span class="ruby-node">&quot;#{ resource }/#{ version }&quot;</span> ]
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
          </div>
        <div class="method">
          <div class="title method-title" id="method-i-flush">
              <b>flush</b>()
            <a href="../../../../../classes/Hoodoo/Services/Discovery/ByDRb/DRbServer.html#method-i-flush" name="method-i-flush" class="permalink">Link</a>
          </div>

            <div class="description">
              <p>Flush out the repository, clearing all stored service records. This is
usually for test purposes only.</p>
            </div>



            <div class="sourcecode">
              <p class="source-link">
                Source:
                <a href="javascript:toggleSource('method-i-flush_source')" id="l_method-i-flush_source">show</a>
              </p>
              <div id="method-i-flush_source" class="dyn-source">
                <pre><span class="ruby-comment"># File lib/hoodoo/services/discovery/discoverers/by_drb/drb_server.rb, line 141</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">flush</span>
  <span class="ruby-ivar">@repository</span> = {}
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
          </div>
        <div class="method">
          <div class="title method-title" id="method-i-ping">
              <b>ping</b>()
            <a href="../../../../../classes/Hoodoo/Services/Discovery/ByDRb/DRbServer.html#method-i-ping" name="method-i-ping" class="permalink">Link</a>
          </div>

            <div class="description">
              <p>Check to see if this DRb service is awake. Returns <code>true</code>.</p>
            </div>



            <div class="sourcecode">
              <p class="source-link">
                Source:
                <a href="javascript:toggleSource('method-i-ping_source')" id="l_method-i-ping_source">show</a>
              </p>
              <div id="method-i-ping_source" class="dyn-source">
                <pre><span class="ruby-comment"># File lib/hoodoo/services/discovery/discoverers/by_drb/drb_server.rb, line 111</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">ping</span>
  <span class="ruby-keyword">return</span> <span class="ruby-keyword">true</span>
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
          </div>
        <div class="method">
          <div class="title method-title" id="method-i-stop">
              <b>stop</b>()
            <a href="../../../../../classes/Hoodoo/Services/Discovery/ByDRb/DRbServer.html#method-i-stop" name="method-i-stop" class="permalink">Link</a>
          </div>

            <div class="description">
              <p>Shut down this DRb service.</p>
            </div>



            <div class="sourcecode">
              <p class="source-link">
                Source:
                <a href="javascript:toggleSource('method-i-stop_source')" id="l_method-i-stop_source">show</a>
              </p>
              <div id="method-i-stop_source" class="dyn-source">
                <pre><span class="ruby-comment"># File lib/hoodoo/services/discovery/discoverers/by_drb/drb_server.rb, line 147</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">stop</span>
  <span class="ruby-identifier">$stop_queue</span>.<span class="ruby-identifier">push</span>( <span class="ruby-keyword">true</span> )
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
          </div>
</div>

    </div>
  </body>
</html>
